package jwanalytics.dbloader;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import junit.framework.TestCase;

/** Junit test for DB class. The test cases are very easy to read for knowers of regular expressions. */
public class DBTest extends TestCase {
	
	/** Logger for all pages. */
	public static Logger logger = Logger.getLogger("jwanalytics");
	
	/** 
	 * @throws Exception */
	public void testGenerateInsertStatement() throws Exception {
		//Test superclass method (abstract superclass) via subclass instance.
		String tablename = "tablename";
		StringBuilder vIns = DB.generateInsertStatement(tablename, new String[]{"column1", "column2"});
		logger.debug("\""+vIns.toString()+"\"");
		String toBeRegExp = " *INSERT +INTO +tablename *\\( *column1 *, *column2 *\\) *VALUES *\\( *\\? *, *\\? *\\) *";
		Pattern p = Pattern.compile(toBeRegExp);
		Matcher m = p.matcher(vIns.toString()); // get a matcher object
		if (!m.matches()) throw new Exception("Insert fails to match regular expression.");
	}

	
	
	/** 
	 * @throws Exception */
	public void testGenerateUpdateStatement() throws Exception {
		//Test superclass method (abstract superclass) via subclass instance.
		String tablename = "tablename";
		StringBuilder vIns = DB.generateUpdateStatement(tablename, new String[]{"column1", "column2"}, new String[] {"column2"});
		logger.debug("\""+vIns.toString()+"\"");
		String toBeRegExp = " *UPDATE +tablename +SET +column1 *= *\\?, *column2 *= *\\? +WHERE +column2 *= *\\? *";
		Pattern p = Pattern.compile(toBeRegExp);
		Matcher m = p.matcher(vIns.toString()); // get a matcher object
		if (!m.matches()) throw new Exception("Insert fails to match regular expression.");
	}
	
}
